package Java.com.wang.Four.Stringg;

public class string {
    private int maxSize=10;
    private char[] chars;
    private int length;

    public string (int n){
        this.maxSize=n;
        this.chars=new char[n];
        this.length=0;
    }

    public int compare(string t){
        int i=0;
        while (this.chars[i]==t.chars[i]&&i<this.length && i<t.getLength()){
            i++;
        }
        if(i==this.length && i==t.length){
            return 0;
        }
        else if(i==t.getLength()&&i<this.length){
            return 1;
        }
        else
            return -1;
    }

public int getLength(){
    return length;
}

public void concat(string t){
    if(this.maxSize<this.length+t.getLength()){
        char[] a =new char [this.length];
        for (int i=0;i<this.length;i++){
            a[i]=this.chars[i];
        }
        this.maxSize=this.length+t.getLength();
        this.chars=new char[this.maxSize];
        for(int i=0; i<a.length;i++){
            this.chars[i]=a[i];
        }
    }
    for(int i=0;i<t.getLength();i++){
        this.chars[this.length]=t.chars[i];
        this.length++;
    }
}

public string subString(int pos,int len){
    if(pos+len>=this.length){
        return null;
    }
    string a =new string(len);
    for (int i=0;i<len;i++){
        a.chars[i]=this.chars[pos+i];
        a.length++;
    }
    return a;
}

public void copy(string t){
    if(this.maxSize<t.maxSize){
        this.maxSize=t.maxSize;
        this.chars = new char[this.maxSize];
    }
    this.length=0;
    for (int i=0;i<t.getLength();i++){
        this.chars[i]=t.chars[i];
        this.length++;
    }
}

public int index(string t){
    if(this.length<t.getLength()){
        return -1;
    }
    int a=-1;
    for(int i=0;i<this.length;i++){
        int j=0;
        while (j<t.getLength()&&this.chars[i+j]==t.chars[j]){
            if(this.chars[i+j]!=t.chars[j]){
                break;
            }
            j++;
        }
        if(j==t.getLength()){
            a=i;
            break;
        }
    }
    return a;
}
}